package com.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSON;
import com.domain.Family;
import com.domain.Jzlx;
import com.domain.Pomr;
import com.service.FamilyService;
import com.service.JmtjService;
import com.service.PomrService;
import com.service.impl.JmtjServerImpl;
import com.utils.Des;
import com.utils.ObjectUtils;
import com.utils.Ret;

@Controller
public class BaseController {
	
	@Autowired
	PomrService pomrService;
	@Autowired
	JmtjService jmtjService;
	@Autowired
	FamilyService familyService;
	
	
	@RequestMapping("/toChangepwdByUcode.do")
	public String toChangepwdByUcode(){
		System.out.println();
		return "changepwd";
	}
	
	@RequestMapping("/doChangepwdByUcode.do")
	public void doChangepwdByUcode(@RequestParam("ucode") int ucode,
			@RequestParam("oldpwd") String oldpwd,
			@RequestParam("newpwd") String newpwd,
			@RequestParam("newpwd2") String newpwd2,
			PrintWriter out){
		
		Des des = new Des();
		oldpwd = des.strDec(oldpwd, "key", null,null);
		newpwd = des.strDec(newpwd, "key", null,null);
		newpwd = des.strDec(newpwd, "key", null,null);
		Ret ret = new Ret(); 
		try {
			ret = pomrService.doChangePwd(ucode, oldpwd,newpwd,newpwd2);
		} catch (Exception e) {
			
			e.printStackTrace();
			ret.setStatusCode("300");
			ret.setMessage("密码修改失败！");
		}
		out.print(ret.toJsonString());
		out.flush();
		out.close();
	}
	
	/**
	 * 1，已经写好的那个，
		2，“查看自己什么病”的接口
		3，自己的登陆信息，（包含登陆密码这些）
		4，体检信息
	 * @param ip
	 * @return
	 */
	boolean validateIP(String ip){
		final String[] portalIPs = {"localhost","10.202.253.130","127.0.0.1","10.202.253.144","10.202.253.145","0:0:0:0:0:0:0:1"};
		boolean result = false;
		for(int i=0;i<portalIPs.length;i++){
			if(portalIPs[i].equals(ip)){
				result = true;
				break;
			}
		}
//		return result;
		return true;
		
	}
	
	@RequestMapping("getPomrInfoByUcode.do")
	public void getPomrInfoByUcode(@RequestParam("ucode") int ucode,PrintWriter out,
			HttpServletRequest request,HttpServletResponse response){
		
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("utf-8");
		String remoteAddr = request.getRemoteAddr();
		
		Map<String,Object> pomrret = new HashMap<String,Object>();
		
		if(!validateIP(remoteAddr)){
			pomrret.put("rcode", "300");
			pomrret.put("message", "ip不合法，无法请求数据！");
		}else{
			
			Map<String,Object> pomr = null;
			Map<String,List<Object>> pomrCascade = null;
			Map<String, List<Map<String, Object>>> pomrCascadeSJ = null;
			try {
				int uid = pomrService.getUidByJkdah(ucode);
				
				pomr = pomrService.getPomrByUid2(uid);
				pomrCascade = pomrService.getPomraCascade(uid);//没有时间的
				pomrCascadeSJ = pomrService.getPomrCascadeSJ(uid);
				
				pomrret.put("rcode", "200");
				pomrret.put("message", "获取信息成功！");
			} catch (Exception e) {
				pomrret.put("rcode", "300");
				pomrret.put("message", "获取信息失败！");
			}

			pomrret.put("pomr", pomr);
			pomrret.put("pomrCascade", pomrCascade);
			pomrret.put("pomrCascadeSJ", pomrCascadeSJ);
			
		}
		
		out.print(JSON.toJSONString(pomrret));
		
	}
	
	@RequestMapping("getJmtjInfoByUcode.do")
	public void getLoginInfoByUcode(@RequestParam("ucode") int ucode,PrintWriter out,
			HttpServletRequest request,HttpServletResponse response){	
		
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("utf-8");
		String remoteAddr = request.getRemoteAddr();
		
		Map<String,Object> pomrret = new HashMap<String,Object>();
		
		if(!validateIP(remoteAddr)){
			pomrret.put("rcode", "300");
			pomrret.put("message", "ip不合法，无法请求数据！");
		}else{
			
			List<Map<String,Object>> jmtjInfo = new ArrayList<Map<String,Object>>();
			try {
				jmtjInfo = jmtjService.getJmtjInfoByUcode(ucode);
				
				pomrret.put("rcode", "200");
				pomrret.put("message", "获取信息成功！");
			} catch (Exception e) {
				pomrret.put("rcode", "300");
				pomrret.put("message", "获取信息失败！");
			}

			pomrret.put("jmtjInfo", jmtjInfo);
			System.out.println(jmtjInfo);
			
		}
		
		out.print(JSON.toJSONString(pomrret));
	}
	
	
	@RequestMapping("getFamilyInfoByUcode.do")
	public void getFamilyInfoByUcode(@RequestParam("ucode") int ucode,PrintWriter out,
			HttpServletRequest request,HttpServletResponse response){	
		
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("utf-8");
		String remoteAddr = request.getRemoteAddr();
		
		Map<String,Object> pomrret = new HashMap<String,Object>();
		
		if(!validateIP(remoteAddr)){
			pomrret.put("rcode", "300");
			pomrret.put("message", "ip不合法，无法请求数据！");
		}else{
			
			Map<String,Object> jmtjInfo = new HashMap<String,Object>();
			try {
				jmtjInfo = familyService.getFamilyInfoByUcode(ucode);
				
				pomrret.put("rcode", "200");
				pomrret.put("message", "获取信息成功！");
			} catch (Exception e) {
				pomrret.put("rcode", "300");
				pomrret.put("message", "该用户不存在家庭档案信息！");
			}

			pomrret.put("jmtjInfo", jmtjInfo);
			System.out.println(jmtjInfo);
			
		}
		
		out.print(JSON.toJSONString(pomrret));
		out.flush();
		out.close();
	}
	
	
}
